package com.xing.bigdata;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by YLZX-Z0107 on 2014/11/4.
 */
public class BigData {
    public static void main(String[] args) {
        BigData bigData = new BigData();
        bigData.getUidFromFile();
    }

    private Set<Long> getUidFromFile() {
        try {
            final int BUFFER_SIZE = 15 * 1024 * 1024;// 缓冲区大小
            Set<Long> setUid = new HashSet<Long>();
            File file = new File("D:\\test\\user_level1.txt");
            MappedByteBuffer inputBuffer = new RandomAccessFile(file, "r").getChannel().map(FileChannel.MapMode.READ_ONLY, file.length()*2/8,file.length()/8);
            byte[] dst = new byte[BUFFER_SIZE];
            long start = System.currentTimeMillis();
            for (int offset = 0; offset < inputBuffer.capacity(); offset += BUFFER_SIZE) {
                if (inputBuffer.capacity() - offset >= BUFFER_SIZE) {
                    for (int i = 0; i < BUFFER_SIZE; i++)
                        dst[i] = inputBuffer.get(offset + i);
                } else {
                    for (int i = 0; i < inputBuffer.capacity() - offset; i++)
                        dst[i] = inputBuffer.get(offset + i);
                }
                String line = new String(dst);
                String[] lines = line.split("\n");
                for (int i = 1; i < lines.length - 1; i++) {
                    if (lines[i].length() > 0) {
                        setUid.add(Long.parseLong(lines[i].trim()));
                    }
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("读取文件文件内容加初始化线程安全容器花费：" + (end - start) + "毫秒 all setUid: " + setUid.size());
            return setUid;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
